const { OPPO_AD_CONFIG } = require("../constants");

module.exports = {
    videoFunc: null,
    interstitialExist: false,
    bannerDict: {},
    adVideoDict: {},
    bannerLoad: [],
    adVideoLoad: [],
    nativeDict: {},
    nativeJson: {},
    init() {
        const sysInfo = qg.getSystemInfoSync();
        if (sysInfo.platformVersionCode < 1051) {
            qg.showToast({
                message: '平台版本过于成旧，请更新大于1052版本'
            })
            return
        }
        this.loadBanner()
        this.initVideo()
        this.initNativeAd()
    },
    // 视频初始化
    initVideo() {
        if (!OPPO_AD_CONFIG.videoId.length) {
            return
        }

        for (let i = 0; i < OPPO_AD_CONFIG.videoId.length; i++) {
            this.adVideoDict[OPPO_AD_CONFIG.videoId[i]] = qg.createRewardedVideoAd({ adUnitId: OPPO_AD_CONFIG.videoId[i] })
            this.adVideoDict[OPPO_AD_CONFIG.videoId[i]].onLoad(() => {
                this.adVideoLoad.push(OPPO_AD_CONFIG.videoId[i])
            })
            this.adVideoDict[OPPO_AD_CONFIG.videoId[i]].onError(err => {
                he.error(err)
            })
            this.adVideoDict[OPPO_AD_CONFIG.videoId[i]].onClose(res => {
                let adExist = this.adVideoLoad.indexOf(OPPO_AD_CONFIG.videoId[i])
                if (adExist >= 0) {
                    this.adVideoLoad.splice(i, 1)
                }
                if (res && res.isEnded || res === undefined) {
                    if (this.videoFunc) {
                        he.local.click("load_ad_get")
                        this.videoFunc()
                        this.videoFunc = null
                    }
                }
                this.loadVideo(OPPO_AD_CONFIG.videoId[i])
            })
        }
        this.loadVideo()
    },
    // 初始化记录原始广告
    initNativeAd() {
        for (let i = 0; i < OPPO_AD_CONFIG.nativeAd.length; i++) {
            let nativeAdId = OPPO_AD_CONFIG.nativeAd[i]
            this.nativeDict[nativeAdId] = qg.createNativeAd({ adUnitId: nativeAdId })
            this.nativeDict[nativeAdId].onLoad((res) => {
                //console.log(res)
                if (res && res.adList) {
                    //这里需要看下res的数据结构是啥，   我这里的处理是获取了adlist的最后一条广告，他每次获取都会棒adlist里面添加一条
                    this.nativeJson[nativeAdId] = res.adList[res.adList.length - 1]
                }
            })
            this.nativeDict[nativeAdId].load()
        }
    },
    // 获得原生
    getNativeJson(id) {
        if (this.nativeJson[id]) {
            let ad_list = this.nativeJson[id]
            for (let i = 0; i < ad_list.length; i++) {
                let nativeCurrentAdItem = ad_list[i]
                let nativeCurrentAdItemId = nativeCurrentAdItem.adId.toString()
                this.nativeDict[id].reportAdShow({ adId: nativeCurrentAdItemId })
            }
            return this.nativeJson[id]
        }
        return []
    },
    //刷新获取原生广告数据，每15s调用一次
    loadNativeAd(id) {
        if (this.nativeJson[id]) {
            this.nativeDict[id].load()
        }
    },
    // 进入原生
    clickNative(id) {
        if (this.nativeDict[id]) {
            let ad_list = this.nativeJson[id]
            for (let i = 0; i < ad_list.length; i++) {
                let nativeCurrentAdItem = ad_list[i]
                let nativeCurrentAdItemId = nativeCurrentAdItem.adId.toString()
                this.nativeDict[id].reportAdClick({ adId: nativeCurrentAdItemId })
                break
            }
            this.nativeDict[id].load()
        }
    },
    // 加载广告
    loadVideo(id) {
        if (id) {
            let adExist = this.adVideoLoad.indexOf(id)
            if (adExist < 0) {
                this.adVideoDict[id].load()
            }
        } else {
            for (let key in this.adVideoDict) {
                this.adVideoDict[key].load()
            }
        }

    },
    // 显示视频
    showVideo(id, fuc) {
        let adExist = this.adVideoLoad.indexOf(id)
        if (adExist < 0) {
            this.loadVideo(id)
            return
        }
        he.local.click("load_ad_click")
        this.videoFunc = fuc
        this.adVideoDict[id].show()
    },
    // 展示Banner形式
    changeBanner(status, posNode) {
        let bannerId = posNode.name
        if (OPPO_AD_CONFIG.bannerId.indexOf(bannerId) < 0) {
            he.error("该广告节点妹有")
            return
        }
        if (status) {
            let posSize = cc.size(posNode.width * zoom_scale, posNode.height * zoom_scale);
            let frameSize = cc.view.getFrameSize();
            let winSize = cc.winSize;
            let worldPos = posNode.convertToWorldSpaceAR(cc.Vec2.ZERO)
            let width = posSize.width / winSize.width * frameSize.width
            let height = posSize.height / winSize.height * frameSize.height
            let left = (worldPos.x - posSize.width / 2) / winSize.width * frameSize.width
            let top = (winSize.height - worldPos.y - posSize.height / 2) / winSize.width * frameSize.width

            this.bannerDict[bannerId].style.left = left
            this.bannerDict[bannerId].style.top = top

            if (width < 300) {
                he.error("广告预制体小了")
            }
            this.bannerDict[bannerId].style.width = width
            this.bannerDict[bannerId].style.height = height
            this.bannerDict[bannerId].show()
            he.log("成功弹出")
        } else {
            this.bannerDict[bannerId].hide()
        }
    },
    // 加载Banner
    loadBanner() {
        if (!OPPO_AD_CONFIG.bannerId.length) {
            return
        }
        for (let i = 0; i < OPPO_AD_CONFIG.bannerId.length; i++) {
            this.bannerDict[OPPO_AD_CONFIG.bannerId[i]] = qg.createBannerAd({ adUnitId: OPPO_AD_CONFIG.bannerId[i], adIntervals: 30 })
            this.bannerDict[OPPO_AD_CONFIG.bannerId[i]].onLoad(() => {
                this.bannerLoad.push(OPPO_AD_CONFIG.bannerId[i])
            })
            this.bannerDict[OPPO_AD_CONFIG.bannerId[i]].onError(err => {
                he.error(err)
            })
        }
    },
}